package cn.itcast.usermanage.controller;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;
@Controller
@RequestMapping("/rest/user")
public class UserRestful {
	@Autowired
	private UserService userService;
	//查询
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	public ResponseEntity<User> queryUserById(@PathVariable("id") Long id){
		try {
			if(id.longValue()<1){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			User user = userService.queryUserById(id);
			if(user==null){
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(user);
			}
			return ResponseEntity.ok(user);
		} catch (Exception e) {
			
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
	}
	//添加;
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ResponseEntity<Void> addUser(User user){
		try {
			if(user==null){
				//204
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
			if(StringUtils.isBlank(user.getUserName())){
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			userService.addUser(user);
			//201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			
			e.printStackTrace();
			//500
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		
	}
	
	//查询回显
	@RequestMapping(method=RequestMethod.GET,value="update")
	public String updateUser(@RequestParam("id") Long id,Model model){
		
		User user = userService.queryUserById(id);
		model.addAttribute("user", user);
		return "userEdit";
	}
	//修改
		@RequestMapping(method=RequestMethod.PUT,value="edit")
		public ResponseEntity<Void> editUser(@RequestParam("id") Long id,User user){
			user.setId(id);
			int i = userService.editUser(user);
			if(i>0){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			
			
			return ResponseEntity.status(HttpStatus.OK).build();
		}
	
	
	
	
	//删除:
	@RequestMapping(method=RequestMethod.DELETE,value="delete")
	public ResponseEntity<Void> updateUser(@RequestParam("ids") List<Object> ids){
		try {
			if(ids==null){
				//204
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			
			userService.deleteUserByIds(ids);
			//204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			
			e.printStackTrace();
			//500
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		
	}
	
	
	//查询
		@RequestMapping(method=RequestMethod.GET,value="/userlist")
		public ResponseEntity<EasyUIResult> queryUserAll(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows){
			try{
			EasyUIResult result = userService.queryUserAllByPage(page, rows);
				if(result==null){
					return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
				}
				return ResponseEntity.ok(result);
			} catch (Exception e) {
				
				e.printStackTrace();
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
			}
		}
}
